/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 *
 *PmIndexTabPermissionController
 * 项目首页Tab权限表
 *
 * 修改纪录
 * 2020-05-18 版本：1.0 liwanpeng 创建。
 * @version 版本：1.0
 * @author 作者：liwanpeng
 * 创建日期：2020-05-18
 */

package com.topscomm.pm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.pm.controller.auto.PmIndexTabPermissionControllerAuto;
import com.topscomm.pm.pojo.PmIndexTabListEntity;
import com.topscomm.pm.pojo.PmIndexTabPermissionEntity;
import com.topscomm.pm.service.impl.PmIndexTabListService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.TapEntity;

import cn.hutool.core.util.ObjectUtil;

import com.topscomm.cbo.model.TreeModel;
import com.topscomm.cbo.pojo.CboRoleEntity;
import com.topscomm.cbo.service.impl.CboRoleService;

/**
 * @author: liwanpeng
 * @date: 2020-05-18
 * @description:项目首页Tab权限表Controller
 * @modify:
 */
@RestController
@RequestMapping("pm/indexTabPermission")
public class PmIndexTabPermissionController extends PmIndexTabPermissionControllerAuto implements IPmIndexTabPermissionController {
	@Resource
	private PmIndexTabListService pmIndexTabListService;
	@Resource
	private CboRoleService cboRoleService;
	/**
	 * @author: liwanpeng
	 * @date: 2020-05-18
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_tabid")) {
			String value = paraMap.get("qry_tabid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and tabid = '{0}' ", value);
		}

		return sql.toString();
	}

	 /**
	 *
	 * @description: 参照查询方法
	 * @param paraMap
	 * @return
	 * @author: liwanpeng
	 * @date: 2020-05-18
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}
	/**
	 * 插入前检查是否有上级tab权限，如果没有则自动添加
	 */
	@Override
	protected void beforeInsert(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		//查询tab树(当前只支持2级tab故只需要查询上级tab即可)
		PmIndexTabListEntity queryById = pmIndexTabListService.queryById(paraMap.get("tabid").toString());
		//查询父tab
		PmIndexTabListEntity parentTab = pmIndexTabListService.queryById(ConvertUtil.convertToString(queryById.getParentid()));
		if (parentTab==null) {
			return;
		}
		//查询是否有此tab权限
		List<PmIndexTabPermissionEntity> queryByWhere = new ArrayList<>();
		try {
			queryByWhere = this.service.queryByWhere("type = '"+paraMap.get("type").toString()+"'"+"and applytocode='"+paraMap.get("applytocode").toString()+"'"+"and tabid='"+parentTab.getId()+"'");

		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		//如果没有则插入
		if (queryByWhere.size()==0 && !"0".equals(paraMap.get("tabid").toString())) {
			//复制一个paraMap，并修改tabid的值插入
			Map<String, Object> cloneParaMap = ObjectUtil.cloneByStream(paraMap);
			cloneParaMap.put("tabid", parentTab.getId());

			TapEntity entity = entityClass.newInstance();
			entity.convertFromMap(cloneParaMap);
			this.service.insert(entity);
		}

		// TODO Auto-generated method stub
		super.beforeInsert(dataMap, paraMap);
	}
	/**
	 *
	 * @description: 树查询方法
	 * @param paraMap
	 * @return
	 * @author: liwanpeng
	 * @date: 2020-05-18
	 * @modify:
	 */

	@GetMapping(value = "/queryTreeList")
	public ResponseResult<Object> queryTreeList(@RequestParam(required = false) Map<String, Object> paraMap) {
//		String sqlWhere = this.spellListSql(paraMap);
//		sqlWhere += " order by code asc";
		List<Map<String, Object>> listModule = pmIndexTabListService.queryMapByWhere("");
		List<Map<String, Object>> listNewModule = new ArrayList<Map<String, Object>>();
		listNewModule.addAll(listModule);
		TreeModel treeModel = new TreeModel();
		List<TreeModel> treeList = treeModel.buildTree(listNewModule, true, "项目首页Tab树");
		Map<String, Object> dataMap = new HashMap<>();
		dataMap.put("dataList", treeList);
		return ResponseResult.ok(dataMap);
	}
}

